import { _decorator, Component, Label } from 'cc';
import { ClientAPI } from './src/api/client.api';
import ServiceAPI from './src/api/service.api';
const { ccclass, property } = _decorator;

@ccclass('NewComponent')
export class NewComponent extends Component {
    @property(Label)
    label: Label = null

    async start() {

        let [local, device_id] = await Promise.all([ClientAPI.Local(), ClientAPI.DeviceID().then(res => res.device_id)]);

        let config = await ServiceAPI.initUser(device_id, 'US');

        console.log(config.userId, device_id, 'US');
        // 启动循环调用
        this.loopRequests(config.userId, 'US');
    }

    /**
   * 循环调用请求方法
   */
    public async loopRequests(userId: number, country: string) {
        let results
        while (true) {
            try {
                const userInfo = await ServiceAPI.userInfo(userId);
                results = `\nUserInfo\n${JSON.stringify(userInfo)}`;
                this.label.string = results;
                const confCountry = await ServiceAPI.ConfCountry(country);
                results = `\nconfCountry\n${JSON.stringify(confCountry)}`;
                this.label.string = results;
                const confCommon = await ServiceAPI.ConfCommon(userId);
                results = `\nconfCommon\n${JSON.stringify(confCommon)}`;
                this.label.string = results;
                const withdrawConfig = await ServiceAPI.WithdrawConfig(userId);
                results = `\nwithdrawConfig\n${JSON.stringify(withdrawConfig)}`;
                this.label.string = results;
                // const newcomerReward = await ServiceAPI.GetNewComerReward(userId);
                const lotteryConfig = await ServiceAPI.LotteryConfig(userId);
                results = `\nlotteryConfig\n${JSON.stringify(lotteryConfig)}`;
                this.label.string = results;
                const passLevelConfig = await ServiceAPI.PassLevelConfig(userId);
                results = `\npassLevelConfig\n${JSON.stringify(passLevelConfig)}`;
                this.label.string = results;
                const passLevelReport = await ServiceAPI.PassLevelReport(userId, 1, 2);
                results = `\npassLevelReport\n${JSON.stringify(passLevelReport)}`;
                this.label.string = results;
                const eliminateReport = await ServiceAPI.EliminateReport(userId);
                results = `\neliminateReport\n${JSON.stringify(eliminateReport)}`;
                this.label.string = results;
                await ClientAPI.CloseLoading();
                results = `\nCloseLoading`
                this.label.string = results;
                const did = await ClientAPI.DeviceID();
                results = `\nDeviceID\n${JSON.stringify(did)}`;
                this.label.string = results;
                await ClientAPI.Float();
                results = `\nFloat`
                this.label.string = results;

            } catch (error) {
                console.error('Error in loopRequests:', error);
            }
        }
    }
}




